package com.xgf.common.xgfcommon.result;

import java.io.Serializable;

import org.apache.http.HttpStatus;

import com.xgf.common.xgfcommon.result.code.ResultEnum;





public class R<T> implements Serializable{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String code;
	
	private String msg;
	
	private T data;
	/**
	 * 防止幂等性问题 
	 */
	private String token;
	
	
	
	
	
	
	public R(String code, String msg) {
		super();
		this.code = code;
		this.msg = msg;
	}


	

	public R(ResultEnum frequencyLimitError) {
		this.code = frequencyLimitError.getReasonCode();
		this.msg = frequencyLimitError.getReasonName();
	}


	public R() {
		this.code = "0";
		this.msg = "success";
	}


	public static R<Object> downgrade() {
		return ResultEnum.build(ResultEnum.FREQUENCY_LIMIT_ERROR);
	}


	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}
	
	
	public static  R ok() {
		return new R<Object>();
	}
	
	
	public static R error() {
		return error(ResultEnum.FAIL);
	}
	
	public static R error(String msg) {
		return error(ResultEnum.FAIL.getReasonCode(), msg);
	}
	
	public static R error(String code, String msg) {
		R r = new R();
		r.setCode(code);
		r.setMsg(msg);
		
		return r;
	}
	
	public static R error(ResultEnum resultEnum) {
		R r = new R();
		r.setCode(resultEnum.getReasonCode());
		r.setMsg(resultEnum.getReasonName());
		
		return r;
	}
	
	

	
	
	
	
}
